<?php

namespace app\admin\controller;

use app\model\ScoreLogModel;
use support\Request;
use support\Response;
use Exception;

class ScoreLogController
{
    /**
     * 获取用户充值记录列表
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            $userId = (int)$request->get('user_id', 0);


            $query = ScoreLogModel::where('user_id', $userId)
                ->orderBy('created_at', 'desc');

            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            $records = [];
            foreach ($list as $item) {
                $records[] = [
                    'id' => $item->id,
                    'user_id'=>$item->user_id,
                    'score'=>$item->score,
                    'type'=>$item->type,
                    'dir'=>$item->dir,
                    'action'=>$item->action,
                    'comment'=>$item->comment,
                    'action_text'=>ScoreLogModel::getActionText($item->action),
                    'created_at' => $item->created_at ? date('Y-m-d H:i:s', $item->created_at) : '-',
                ];
            }

            return json([
                'data' => [
                    'list' => $records,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
}